package com.cn.hotel.servlet.back.user;

import com.alibaba.fastjson.JSONObject;
import com.cn.hotel.model.PageModel;
import com.cn.hotel.model.UserModel;
import com.cn.hotel.service.IUserService;
import com.cn.hotel.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/back/user/findAll")
public class userFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理乱码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        /**得到页面结果*/
        String username = req.getParameter("username");
        String phone = req.getParameter("phone");
        String sex = req.getParameter("sex");
        String hobby = req.getParameter("hobby");
        String startBirthday = req.getParameter("startBirthday");
        String enBirthday = req.getParameter("endBirthday");
        String introduce = req.getParameter("introduce");
        String pageSize = req.getParameter("pageSize");
        String pageNum = req.getParameter("pageNum");
        PageModel pageModel = new PageModel();
        pageModel.setPageSize(pageSize == null||"".equals(pageSize)?10:Integer.parseInt(pageSize));
        pageModel.setPageNum(pageNum == null||"".equals(pageNum)?1:Integer.parseInt(pageNum));
        /**封装*/
        UserModel userModel = new UserModel();
        userModel.setUsername(username);
        userModel.setPhone(phone);
        userModel.setSex(sex);
        userModel.setHobby(hobby);
        userModel.setStartBirthday(startBirthday);
        userModel.setEndBirthday(enBirthday);
        userModel.setIntroduce(introduce);
        /**调用服务层*/
        IUserService iUserService = new UserServiceImpl();
        /**返回数据*/
        List<UserModel> list = iUserService.findAll(userModel);
        int number = iUserService.getCount(userModel);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", list);
        jsonObject.put("number",number);
        /** 给页面反馈*/
        resp.getWriter().println(jsonObject);
    }
}
